﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RuleCode.Table;

namespace chatblog
{
    public partial class selbbszhiding : System.Web.UI.Page
    {
        string ParentID = "1";
        cbcata c;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                BindTree();
            }
        }
        private void BindTree()
        {
            cbarea area;
            area = cbarea.getcurarea();
            ParentID = area.cbareaid.ToString();

            TreeView1.Nodes.Clear();
            c = cbcata.Find("( cbstatus = '" + area.cbareaid.ToString()
                + "' or cbstatus = '全部')");
            BindTreeNode(TreeView1.Nodes, ParentID);
            TreeView1.ExpandAll();
        }

        private int BindTreeNode(TreeNodeCollection nodeCollection, string parentNodeId)
        {
            string filter = " pid1='" + parentNodeId
                + "'";
            DataRow[] dRows = c.Select(filter);
            int count = 0;

            foreach (DataRow dRow in dRows)
            {
                count++;
                TreeNode childNode;
                childNode = new TreeNode();
                childNode.Value = dRow["cbcataid"].ToString();
                childNode.Text = dRow["cbcataid"].ToString() + " " + dRow["cbname"].ToString();

                int childCount = BindTreeNode(childNode.ChildNodes, dRow["cbcataid"].ToString());
                if (childCount > 0)
                    nodeCollection.AddAt(0, childNode);
                else
                    nodeCollection.Add(childNode);
            }
            return count;
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (Request["id"] != null && this.TreeView1.SelectedNode != null)
            {
                if (RuleCode.Table.cbcata.canmanage(this.TreeView1.SelectedNode.Value))
                {
                    string sql = " update cblist set zhidingcataid = '"
                        + this.TreeView1.SelectedNode.Value + "' where (1=2)  "
                        + Request["id"].ToString();
                    RuleCode.Common.SqlHelper.Instance.ExecuteNonQuery(CommandType.Text, sql);
                    ScriptManager.RegisterStartupScript(this.TreeView1, this.TreeView1.GetType(), "hasSelected",
                        "<script type =\"text/javascript\" >treeViewSelectChanged('" + Request["id"]
                        + "','" + this.TreeView1.SelectedNode.Value + "');</script>", false);
                }
                else
                {
                    MessageBox.Show(this.Page, "你没有操作该模块的权限.");
                }
            }
            else
            {
                MessageBox.Show(this.Page, "操作不成功.");
            }
        }
    }
}
